package com.app.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.app.common.Result;
import com.app.model.*;
import com.app.service.*;
import com.app.utils.CommonHelp;
import com.app.utils.TokenUtils;
import com.app.vo.IntegralGoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 积分商品
 *
 * @author 16693103420
 * @since 2024-02-01
 */
@RestController
@RequestMapping("/integralGoods")
public class IntegralGoodsController {
    @Autowired
    private IntegralGoodsService integralGoodsService;
    @Autowired
    private UserService userService;
    @Autowired
    private FileService fileService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private AdminUserService adminUserService;

    // 查询列表
    @GetMapping("/getIntegralGoodsList")
    public Result getIntegralGoodsList(@RequestParam(defaultValue = "0") Integer page,
                                       @RequestParam(defaultValue = "10") Integer limit,HttpServletRequest request,
                                       @RequestParam(required = false) Integer areaId,
                                       @RequestParam(required = false) String keyword) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        IntegralGoodsVo IntegralGoodsVo = integralGoodsService.getIntegralGoodsList(page, limit,user,areaId,keyword);
        return Result.success(IntegralGoodsVo);
    }
    // 查询列表
    @GetMapping("/getIntegralGoodsListUser")
    public Result getIntegralGoodsListUser(@RequestParam(defaultValue = "0") Integer page,
                                       @RequestParam(defaultValue = "10") Integer limit,HttpServletRequest request) {
//        String token = request.getHeader("token");
//        if (token == null) {
//            return Result.error(405, "token不存在");
//        }
//        String openId = TokenUtils.verify(token);
//        UserModel userModel = userService.getUserByOpenid(openId);
//        if (userModel == null) {
//            return Result.error(405, "用戶不存在");
//        }
        IntegralGoodsVo IntegralGoodsVo = integralGoodsService.getIntegralGoodsList(page, limit,request);
        return Result.success(IntegralGoodsVo);
    }
    //根据id查询
    @GetMapping("/getIntegralGoodsById")
    public Result getIntegralGoodsById(Integer id) {
        IntegralGoodsModel integralGoods = integralGoodsService.getById(id);
        if (StrUtil.isNotBlank(integralGoods.getAreaCode())) {
            List<String> list = JSON.parseObject(integralGoods.getAreaCode(), new TypeReference<List<String>>() {
            });
            integralGoods.setValue(list);
        }
        //文件id转义
        List<FileModel> fileModelList = new ArrayList<>();
        if (StrUtil.isNotBlank(integralGoods.getFileId())) {
            String[] split = integralGoods.getFileId().split(",");
            for (String s : split) {
                FileModel fileModel = fileService.getById(s);
                fileModelList.add(fileModel);
            }
            integralGoods.setFile(fileModelList);
        }
        return Result.success(integralGoods);
    }

    //新增和修改
    @PostMapping("/addAndUpdateIntegralGoods")
    public Result addAndUpdateIntegralGoods(@RequestBody IntegralGoodsModel integralGoodsModel) {
        IntegralGoodsModel integralGoods = integralGoodsService.getById(integralGoodsModel.getId());
        if (integralGoodsModel.getValue() != null) {
            integralGoodsModel.setAreaCode(JSON.toJSONString(integralGoodsModel.getValue()));
        }
        if (integralGoods != null) {
            integralGoodsModel.setUpdateTime(CommonHelp.getNowTimestamp());
            integralGoodsService.updateById(integralGoodsModel);
            return Result.success("修改成功");
        } else {
            integralGoodsModel.setCreateTime(CommonHelp.getNowTimestamp());
            integralGoodsService.save(integralGoodsModel);
            return Result.success("添加成功");
        }
    }

    //兑换商品
    @PostMapping("/exchangeGoods")
    public Result exchangeGoods(@RequestBody IntegralGoodsModel integralGoodsModel, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        Integer userId = userModel.getId();
        if (integralGoodsModel.getAddressId() == null) {
            return Result.error(201, "请选择收货地址");
        }
        String message = integralGoodsService.exchangeGoods(integralGoodsModel, userId);
        if ("兑换成功".equals(message)) {
            return Result.success(message);
        } else {
            return Result.error(201, message);
        }
    }

    //根据id删除
    @GetMapping("/delIntegralGoods")
    public Result delIntegralGoods(Integer id) {
        IntegralGoodsModel goodsModel = integralGoodsService.getById(id);
        if (goodsModel != null) {
            goodsModel.setStatus(2);
            integralGoodsService.updateById(goodsModel);
        }
        return Result.success("删除成功");
    }
}

